

	var url1="http://led.smartcld.cn/api/SysApi/GetAllRecords?type=1&size=100&date=";;
	var url2="http://led.smartcld.cn/Company/GetListWithPager?_search=false&nd=1559317456755&rows=1500&page=1&sidx=&sord=asc";
	var url3="http://led.smartcld.cn/hardware/GetListWithPager?_search=true&nd=1557237192508&rows=15&page=1&sidx=&sord=asc";
	var url4="http://led.smartcld.cn/statistics/GetListWithPager?orginationName=&reportType=0";


//页面加载事件
$(document).ready(function(){
	   
	 getWorkerInfo();
	 getWorkerInfoYesterday();
	 getOrgCount();
	 getToolInfo();
	 getEmployerInfo(); 
	setInterval("getWorkerInfo()",20000);
	setInterval("getToolInfo()",20000);  
	
})

//请求人员统计数据
function getWorkerInfo(){ 
	//var url="http://led.smartcld.cn/api/SysApi/GetAllRecords?type=1&size=100&date=";
	var today = getDate(0);  
	var todayUrl = url1 + today;
	$.get(todayUrl, function(objJson){
		//alert(JSON.stringify(objJson));  
		if($.isEmptyObject(objJson)){
			return;
		} 		   
		divCountOneShow(objJson); //累计
		divCountTwoShow(objJson); //员工
		divCountThreeShow(objJson); //访客
		
		//统计图表 
		echartTwoShow(objJson); 
	
  }, "json");
	
	
}

function getOrgCount(){
	//var url="http://led.smartcld.cn/Company/GetListWithPager?_search=false&nd=1559317456755&rows=1500&page=1&sidx=&sord=asc";
	 
	$.get(url2, function(objJson){
		//alert(JSON.stringify(objJson));  
		if($.isEmptyObject(objJson)){
			return;
		} 		    
		divCountFourShow(objJson); //施工单位 
	
  }, "json");
}
//昨天的数据
function getWorkerInfoYesterday(){ 
	//var url="http://led.smartcld.cn/api/SysApi/GetAllRecords?type=1&size=100&date=";
	var yesterday =  getDate(1);
	var yesterdayUrl = url1 + yesterday;
	$.get(yesterdayUrl, function(objJson){
		if($.isEmptyObject(objJson)){
			return;
		} 		 
		//alert(JSON.stringify(objJson));  
		//统计图表 
		echartThreeShow(objJson);  
		
  }, "json");
}

//请求设备统计数据
function getToolInfo(){ 
	//var url="http://led.smartcld.cn/hardware/GetListWithPager?_search=true&nd=1557237192508&rows=15&page=1&sidx=&sord=asc";
	$.get(url3, function(objJson){
		if($.isEmptyObject(objJson)){
			return;
		} 	
		//alert(JSON.stringify(objJson)); 
		echartOneShow(objJson);
		echartToolList(objJson);
		
  }, "json");

}
//请求员工数据, 岗位列表
function getEmployerInfo(){
	//var url="http://led.smartcld.cn/statistics/GetListWithPager?orginationName=&reportType=0";
	$.get(url4, function(objJson){
		if($.isEmptyObject(objJson)){
			return;
		} 	
		//alert(JSON.stringify(objJson));
		divListShow(objJson);
		
  }, "json");
}



//div_echart_one展示的统计图标
function echartOneShow(objJson){
	var onlineCount = parseInt(resolvingEquipmentJson(objJson, 1));
	var offlineCount = parseInt(resolvingEquipmentJson(objJson, 2));
	
	var dom = document.getElementById("div_echart_one");
	var myChart = echarts.init(dom);
	var app = {};
	option = null;
	option = {
		tooltip : {
			trigger: 'item',
			formatter: "{a} <br/>{b} : {c} ({d}%)"
		}, 
		series : [
			{
				name: '设备统计',
				type: 'pie',
				radius : ['45%','65%'],
				center: ['50%', '56%'],
				
												 
				data:[
					{value:parseInt(onlineCount), name:'在线'},
					{value:parseInt(offlineCount), name:'离线'}
				],
				itemStyle: {
					emphasis: {
						shadowBlur: 10,
						shadowOffsetX: 0,
						shadowColor: 'rgba(0, 0, 0, 0.5)'
					},
					normal:{
								color:function(params) {
								var colorList = [          
										'#19b191', '#c2c2c2'
									];
									return colorList[params.dataIndex]
								 },
								 labelLine: {
									show: true,
									length:0.7
								  }
							}
				}
			}
		]
	};
	if (option && typeof option === "object") {
		myChart.setOption(option, true);
	}
}

function echartToolList(objJson){
	var inOnline = resolvingEquipmentJson(objJson,4);
	var inOffline =resolvingEquipmentJson(objJson,5);
	var outOnline = resolvingEquipmentJson(objJson,6);
	var outOffline = resolvingEquipmentJson(objJson,7);
	 
	$("#td_in_online").html(inOnline + "台在线");
	$("#td_in_offline").html(inOffline + "台离线");
	$("#td_out_online").html(outOnline + "台在线");
	$("#td_out_offline").html(outOffline + "台离线");
	
}

//div_echart_two展示的统计图标
function echartTwoShow(objJson){
		
	var yuangong = parseInt(resolvingEmployeeJson(objJson,2));
	var fangke =parseInt(resolvingEmployeeJson(objJson,3));
	var moshengren=parseInt(resolvingEmployeeJson(objJson,4)); 
	
	$("#span_two_yuangong").html(yuangong);
	$("#span_two_fangke").html(fangke);
	$("#span_two_moshengren").html(moshengren);
	
		
	var dom = document.getElementById("div_echart_two");
	var myChart = echarts.init(dom);
	var app = {};
	option = null;
	option = {
		
		tooltip : {
			trigger: 'item',
			formatter: "{a} <br/>{b} : {c} ({d}%)"
		}, 
		series : [
			{
				name: '今日签到统计',
				type: 'pie',
				radius : ['45%','65%'],
				center: ['50%', '56%'],
				data:[
					{value:parseInt(yuangong), name:'员工'},
					{value:parseInt(fangke), name:'访客'},
					{value:parseInt(moshengren), name:'陌生人'}
				],
				itemStyle: {
					emphasis: {
						shadowBlur: 10,
						shadowOffsetX: 0,
						shadowColor: 'rgba(0, 0, 0, 0.5)'
					},
					normal:{
								color:function(params) {
								var colorList = [          
										 '#f6ab00','#18334e','#d90051'
									];
									return colorList[params.dataIndex];
								 },
								 labelLine: {
									show: true,
									length:0.7
								  }
							}
				}
			}
		]
	};
	if (option && typeof option === "object") {
		myChart.setOption(option, true);
	}
}


//div_echart_three展示的统计图标
function echartThreeShow(objJson){
		
	var yuangong = parseInt(resolvingEmployeeJson(objJson,2));
	var fangke =parseInt(resolvingEmployeeJson(objJson,3));
	var moshengren=parseInt(resolvingEmployeeJson(objJson,4)); 
	
	$("#span_three_yuangong").html(yuangong);
	$("#span_three_fangke").html(fangke);
	$("#span_three_moshengren").html(moshengren);
	
	var dom = document.getElementById("div_echart_three");
	var myChart = echarts.init(dom);
	var app = {};
	option = null;
	option = {
		
		tooltip : {
			trigger: 'item',
			formatter: "{a} <br/>{b} : {c} ({d}%)"
		},
		series : [
			{
				name: '昨日考勤情况',
				type: 'pie',
				radius : ['45%','65%'],
				center: ['50%', '56%'],
				data:[
					{value:parseInt(yuangong), name:'员工'},
					{value:parseInt(fangke), name:'访客'},
					{value:parseInt(moshengren), name:'陌生人'}
				],
				itemStyle: {
					emphasis: {
						shadowBlur: 10,
						shadowOffsetX: 0,
						shadowColor: 'rgba(0, 0, 0, 0.5)'
					},
					normal:{
								color:function(params) {
								var colorList = [          
										 '#f6ab00','#18334e','#d90051'
									];
									return colorList[params.dataIndex];
								 },
								 labelLine: {
									show: true,
									length:0.7
								  }
							}
				}
			}
		]
	};
	if (option && typeof option === "object") {
		myChart.setOption(option, true);
	}
}

//div_count_one展示统计数据，累计
function divCountOneShow(objJson){
	var num = parseInt(resolvingEmployeeJson(objJson,1));
	  //num = parseInt(Math.round(Math.random()*100000)); 
	var arrChar = commSplitToChar(num);
	for(var i=0; i<arrChar.length; i++){
		$("#li_count_one_"+String(i+1)).html(arrChar[i]);
	}
}




//li_count_two_ 展示统计数据， 员工
function divCountTwoShow(objJson){
   var num = parseInt(resolvingEmployeeJson(objJson,2)); 
    var arrChar = commSplitToChar(num);
    for(var i=0; i<arrChar.length; i++){
        $("#li_count_two_"+String(i+1)).html(arrChar[i]);
    }
}



//li_count_three_ 展示统计数据，访客
function divCountThreeShow(objJson){
    var num = parseInt(resolvingEmployeeJson(objJson,3));  
    var arrChar = commSplitToChar(num);
    for(var i=0; i<arrChar.length; i++){
        $("#li_count_three_"+String(i+1)).html(arrChar[i]);
    }
}



//li_count_four_ 展示统计数据， 施工单位
function divCountFourShow(objJson){
    var num = parseInt(resolvingOrgJson(objJson)); 
    var arrChar = commSplitToChar(num);
    for(var i=0; i<arrChar.length; i++){
        $("#li_count_four_"+String(i+1)).html(arrChar[i]);
    }
}


//div_list 展示列表数据, 岗位
function divListShow(objJson){
	//var str ='{"records":0,"rows":[{"organizationId":0,"organizationName":"第一次公司","workerCount":0,"employers":{"施工岗":1,"管理岗位":3, "人事":5,"运维":7,"性质嗯":67,"十大":98,"卡的":86},"status":1,"state":"启用"}],"page":0,"pagesize":10,"total":0}';
	
	// 将字符串转换为JSON对象
	//var objJson = JSON.parse(str);
	var obj = objJson.rows[0].employers;
	
	var html= "";
	var count=0;
	for(var key in obj){ count++;}
	if(count<3){
		html += '<ul>';
		for(var key in obj){ 
			html += '<li>'+ key + '： ' + obj[key] +'人</li>';
		} 
		html += '</ul>';
	}else{
		var tdCount=0;
		for(var key in obj){
			tdCount++;
			if(tdCount<=3){
				if(tdCount==1){
					html += '<ul>'; 
				}
				html += '<li>'+ key + '： ' + obj[key] +'人</li>';
				if(tdCount==3){
					html += '</ul>'; 
				}
			}else{ 
				if(tdCount%3==1){
					html += '<ul>';
				}
				html += '<li>'+ key + '： ' + obj[key] +'人</li>'; 
				if(tdCount%3==0){
					html += '</ul>';
				} 
			}   
		}
	} 
	
	$("#div_list_table").html(html);
}







//------------------------------------------------------------------------------------------------------
//拆分字符串到数组， 返回一个数组
function commSplitToChar(num){ 
	if(parseInt(num)>99999){
			num = '99999';
	}
	num = formatNum(parseInt(num), 5);
	var mycars=new Array() 
	for(var i=0;i<num.length;i++){ 
	 	mycars[i] = num.charAt(i);
	} 
	return mycars;
}
//格式化数字
function formatNum(num, length) {
    return (num/Math.pow(10,length)).toFixed(length).substr(2);
}
 
//解析设备统计的json
function resolvingEquipmentJson(objJson, type){
	//var str ='{"records":3,"rows":[{"id":1,"name":"进","deviceId":"2","status":0,"location":null,"direction":1,"deviceSn":"SKP-b9f64268cb117f99d15487db71bcec9a","state":"启用","groupIds":null},{"id":2,"name":"出","deviceId":"3","status":1,"location":null,"direction":2,"deviceSn":"SKP-3fc913db60b1afc3ae5bd1cc60b87ba5","state":"启用","groupIds":null},{"id":3,"name":"ddddd","deviceId":null,"status":1,"location":"dddddd","direction":1,"deviceSn":"ddddd","state":"启用","groupIds":null}],"page":1,"pagesize":15,"total":1}';

	// 将字符串转换为JSON对象
	//var objJson = JSON.parse(str);
	 //设备总数
	var equipmentCount=0;
	var inOnline=0;
	var inOffline=0;
	var outOnline=0;
	var outOffline=0;
	var onlineCount=0;
	var offline = 0;
	
	try{
	  equipmentCount = objJson.records;
	 $.each(objJson.rows, function (n, obj) {
			if(obj.status == '1'){
				onlineCount++;
				if(obj.direction=='1'){
					inOnline++;
				}else{
					outOnline++;
				}
			}else{
				offline++;
				if(obj.direction=='1'){
					inOffline++;
				}else{
					outOffline++;
				}
			}
			
		});		
	}catch(err){
		return 0;	
	}
	 
	 //返回可用值
	 switch(type){
		case 1:
		 //在线总数
		  return onlineCount;
		case 2:
		  //离线总数
		  return offline;
		case 3:
			//设备总数
			return equipmentCount;
		case 4: 
			return inOnline;
		case 5: 
			return inOffline;
		case 6: 
			return outOnline;
		case 7:
			return outOffline;
			
		default:
		  return "";
	}
}

//解析人员统计数据， 员工，访客，陌生人
function resolvingEmployeeJson(objJson,type){
	//var str ='{"code":200,"message":"OK","data":{"total":4,"pageNum":1,"size":4,"pageSize":100,"totalPage":1,"data":[{"userId":5,"name":"","avatar":"","icNumber":"","idNumber":"","jobNumber":"","receptionUserId":0,"receptionUserName":"","groupId":0,"groupName":"","sn":"SKP-b9f64268cb117f99d15487db71bcec9a","deviceName":"","direction":0,"signAvatar":"5ccea2cf7d567b00010ecc51","signBgAvatar":"5ccea2cf7d567b00010ecc52","signDate":"2019-05-05","signTime":1557045951,"type":1},{"userId":7,"name":"","avatar":"","icNumber":"","idNumber":"","jobNumber":"","receptionUserId":0,"receptionUserName":"","groupId":0,"groupName":"","sn":"SKP-b9f64268cb117f99d15487db71bcec9a","deviceName":"","direction":0,"signAvatar":"5ccea2c27d567b00010ecc4f","signBgAvatar":"5ccea2c27d567b00010ecc50","signDate":"2019-05-05","signTime":1557045938,"type":1},{"userId":7,"name":"","avatar":"","icNumber":"","idNumber":"","jobNumber":"","receptionUserId":0,"receptionUserName":"","groupId":0,"groupName":"","sn":"SKP-b9f64268cb117f99d15487db71bcec9a","deviceName":"","direction":0,"signAvatar":"5ccea0e57d567b00010ecc4b","signBgAvatar":"5ccea0e57d567b00010ecc4c","signDate":"2019-05-05","signTime":1557045461,"type":1},{"userId":5,"name":"","avatar":"","icNumber":"","idNumber":"","jobNumber":"","receptionUserId":0,"receptionUserName":"","groupId":0,"groupName":"","sn":"SKP-b9f64268cb117f99d15487db71bcec9a","deviceName":"","direction":0,"signAvatar":"5ccea0de7d567b00010ecc49","signBgAvatar":"5ccea0de7d567b00010ecc4a","signDate":"2019-05-05","signTime":1557045454,"type":1}]}}';

	// 将字符串转换为JSON对象
	//var objJson = JSON.parse(str);
	
	var allPeople= 0;
	var yuanGong=0;
	var fangKe=0;
	var moShengRen=0;
	
	try{
	 var newJson = removeRepeatData(objJson); 
	 $.each(newJson, function (n, obj) {
			allPeople++;
			if(obj.type == '1'){
				yuanGong++;
			}else if(obj.type == '2'){
				fangKe++;
			}else{
				moShengRen++;
			}
		});
	}catch(err){
		return 0;	
	}
	  //返回可用值
	 switch(type){
		case 1:
			//累计
			return allPeople; 
		case 2:
		 //员工
		  return yuanGong; 
		case 3:
		//访客
		  return fangKe;
		case 4:
			//陌生人
			return moShengRen;
		default:
		  return "";
	}
}

//去掉重复数据
function removeRepeatData(objJson){
	var newJson =[];
	 var arr = new Array();  
	 var n=0;
	 try{
		 $.each(objJson.data.data, function (n, obj) {
			var flag = 1;
			//判断是否有重复的
			 for( var i in arr){
				if(arr[i]==obj.userId){
					flag = 0;
					break;
				}
			}
			if(flag == 1){ 
				var j = {};
				 j.userId = obj.userId;
				 j.name = obj.name;
				 j.type=obj.type;
				 newJson.push(j);
				arr[n] = obj.userId;
				n++;
			} 
		 }); 
	 }catch(err){
		 
	}
	return newJson; 
	  
	
}

//获取当前日期
 function getDateStr(AddDayCount){
	var dd = new Date();
	dd.setDate(dd.getDate()+AddDayCount); 
	var y = dd.getFullYear();
	var m = dd.getMonth()+1; 
	var d = dd.getDate();
	return y+'-'+(m<10?'0'+m:m)+'-'+d;
}


function getDate(flag){
	if(flag==0){
		return getDateStr(0);
	}else{
		return getDateStr(-1);
	}
}


//解析施工单位
function resolvingOrgJson(objJson){
	//var str ='{"records":70,"rows":[{"id":25,"name":"中安保","organizationId":11,"organizationName":"二期施工区南区","address":"三门核电厂南区施工区","contact":"张善利","contactPhoneNumber":"12345678912","status":2,"state":"离场"}],"page":1,"pagesize":1500,"total":1}';

	// 将字符串转换为JSON对象
	//var objJson = JSON.parse(str);
	
	return objJson.records;
	 
}



	
	
	